/**
 * @UiApp.cpp
 * Copyright 2013 (C) FatalEror / Dean Bahari
 * 
 * UiPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * See LICENSE file in main folder for more information.
 */

#include "UiCommon.hpp"
#include "UiApp.hpp"
#include "UiBase.hpp"
#include "UiControl.hpp"
#include "UiResource.hpp"

lpfnDwmSetWindowAttribute UiApp::DwmSetWindowAttribute;
UiApp *UiApp::currentApp = NULL;
int    UiApp::OSVersion  = -1;
UiApp::UiApp()
{
	Gdiplus::GdiplusStartupInput gdiStartup;
	Gdiplus::GdiplusStartup(&mGdiToken, &gdiStartup, NULL);
	mResource  = new UiResource();

	UiApp::currentApp = this;

	HDC hDC = GetDC(NULL);
	mDCBuffer = CreateCompatibleDC(hDC);
	mBMBuffer = CreateCompatibleBitmap(hDC, 2 << 10, 2 << 10);
	mOldBitmap = SelectObject(mDCBuffer, mBMBuffer);
	ReleaseDC(NULL, hDC);

	WNDCLASSEX winClass;
	winClass.cbSize        = sizeof(winClass);
	winClass.style         = 0;
	winClass.lpfnWndProc   = UiBase::handleWindowProc;
	winClass.cbClsExtra    = 0;
	winClass.cbWndExtra    = 0;
	winClass.hInstance     = GetModuleHandle(0);
	winClass.hIcon         = 0;
	winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
	winClass.lpszMenuName  = 0;
	winClass.lpszClassName = L"UiBase";
	winClass.hIconSm       = 0;
	RegisterClassEx(&winClass);

	winClass.style         = CS_DROPSHADOW;
	winClass.lpszClassName = L"UiTooltip";
	RegisterClassEx(&winClass);

	OSVERSIONINFO osvi;
	ZeroMemory(&osvi, sizeof(osvi));
	osvi.dwOSVersionInfoSize = sizeof(osvi);

	GetVersionEx(&osvi);

	if (osvi.dwMajorVersion == 5) {
		switch (osvi.dwMinorVersion) {
		case 0:
			UiApp::OSVersion = UiApp::UI_OS_WIN2000;
			break;
		case 1:
			UiApp::OSVersion = UiApp::UI_OS_WINXP;
			break;
		default:
			UiApp::OSVersion = -1;
			break;
		}
	} else if (osvi.dwMajorVersion == 6) {
		switch (osvi.dwMinorVersion) {
		case 0:
			UiApp::OSVersion = UiApp::UI_OS_WINVISTA;
			break;
		case 1:
			UiApp::OSVersion = UiApp::UI_OS_WIN7;
			break;
		case 2:
			UiApp::OSVersion = UiApp::UI_OS_WIN8;
			break;
		default:
			UiApp::OSVersion = -1;
			break;
		}
	}

	if (UiApp::getOSVersion() >= UiApp::UI_OS_WINVISTA) {
		HINSTANCE hApi32 = LoadLibrary(L"dwmapi.dll");
		UiApp::DwmSetWindowAttribute = (lpfnDwmSetWindowAttribute)GetProcAddress(hApi32, "DwmSetWindowAttribute");
	}
}

UiApp::~UiApp()
{
	delete mResource;
	Gdiplus::GdiplusShutdown(mGdiToken);
	SelectObject(mDCBuffer, mOldBitmap);
	DeleteObject(mBMBuffer);
	DeleteDC(mDCBuffer);
}

int UiApp::run()
{
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.message;
}

void UiApp::quit()
{
	for (unsigned int i = 0; i < UiBase::baseItem.size(); i++)
		UiBase::baseItem[i]->close();
	PostQuitMessage(0);
}

HDC UiApp::getDCBuffer()
{
	return mDCBuffer;
}

HBITMAP UiApp::getBitmapBuffer()
{
	return mBMBuffer;
}

UiApp *UiApp::getCurrentApp()
{
	return UiApp::currentApp;
}

UiResource *UiApp::getResource()
{
	return UiApp::currentApp->mResource;
}

int UiApp::getOSVersion()
{
	return UiApp::OSVersion;
}